$(document).ready(function () {
    var table = $('table#table').DataTable({
        ajax: {
            url: baseUrl + '/datatable',
            data : function(d) {
                d.headerId = headerId;
                return d;
            }
        },
        searchCols: [{
            search: headerId
        }],
        order: [
            [1, 'asc']
        ],
        columns: [{
            data: 'headerId',
            data: 'headerId',
            defaultContent: '',
            visible: false
        },{
            data: 'id',
            sortable: false,
            defaultContent: '',
            'class': 'text-center',
            render: function (data, type, row, meta) {
                return '<div class="checkbox"><input type="checkbox" name="ids" value="' + data + '"/><label for="ids"/></div>'
            }

        },{
            name: 'header.documentNumber',
            data: 'header.documentNumber'
        },{
            name: 'lineNumber',
            data: 'lineNumber'
        },{
            name: 'materialCode',
            data: 'materialCode'
        },{
            name: 'materialName',
            data: 'materialName'
        },{
            name: 'qtyOrdered',
            data: 'qtyOrdered'
        },{
            name: 'qtyAllocated',
            data: 'qtyAllocated'
        },{
            name: 'qtyPicked',
            data: 'qtyPicked'
        },{
            name: 'qtyShipped',
            data: 'qtyShipped'
        },{
            name: 'lotattr07',
            data: 'lotattr07'
        },{
            name: 'lineStatus',
            data: 'lineStatus',
            render: function (data, type, row, meta) {
                return VALUES_MAP_DICT_TYPE && VALUES_MAP_DICT_TYPE[data] ? VALUES_MAP_DICT_TYPE[data].text : '';
            }
        }, {
            data: "id",
            defaultContent: '',
            sortable: false,
            'class': 'text-center',
            render: function (data, type, row, meta) {
                return ('<div class="btn-group">' +
                '<a  class="btn btn-default btn-xs" title="修改" href="' + baseUrl + '/edit/' + row.id + '"><i class="fa fa-edit"></i></a>' +
                '<a class="btn btn-default btn-xs del" title="删除" href="#"><i class="fa fa-trash-o"></i></a>' +
                '</div>' +
                '&nbsp;&nbsp;');
            }
        }]
    });

    table.on('click', '.del', function () {
        var id = table.row($(this).parents('tr')).data().id;
        $("#delForm #ids").val(id);
        if (id) {
            bootbox.confirm("确定要删除数据吗?", function (result) {
                if (result) {
                    $("#delForm").submit();
                }
            });
        }
    });
    $(".btn.delAll").on("click", function (e) {
        var ids = [];
        $('#table>tbody input:checked').each(function (i, item) {
            ids.push($(item).val());
        });
        $("#delForm #ids").val(ids.join(","));
        if (ids.length > 0) {
            bootbox.confirm("确定要删除选中的数据吗?", function (result) {
                if (result) {
                    $("#delForm").submit();
                }
            });
        } else {
            bootbox.alert("请选择要删除的数据.");
        }
    });

    table.on('click', '.enable', function () {
        var that = this;
        var data = table.row($(this).parents('tr')).data();
        var id = data.id;
        var enabled = data.enabled;
        bootbox.confirm("确定要更新选中的数据吗?", function(resul) {
            if (resul) {
                $.get(baseUrl + "/enable", {
                    enabled: !enabled,
                    ids: id
                }, function (res) {
                    if (res) {
                        data.enabled = !enabled;
                        $(that).find("i").removeClass("fa-" + (enabled ? "check-" : "") + "square-o").addClass("fa-" + (!enabled ? "check-" : "") + "square-o");
                        toastr.info('操作成功!');
                    }

                });
            }});
    });

    $('.search-form').on("submit", function (e) {
        table.draw();
        //阻止表单submit
        return false;
    });

});
